﻿#pragma once
#include<iostream>
#include<string>
using namespace std;
namespace yjt
{
	class string
	{
	public:


		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		//const
		typedef char* const_iterator;
		const_iterator begin()const
		{
			return _str;
		}
		const_iterator end()const
		{
			return _str + _size;
		}
		//1.构造函数
		string(const char* str = "")
			//:_str(new char[strlen(str) + 1])
			//, _size(strlen(str))
			//,_capacity(strlen(str))
			//缺陷：每次创建都需要strlen，复杂多太高了
			//修改1
			//: _capacity(strlen(str))
			//, _size(strlen(str))
			//, _str(new char[_capacity + 1])
			//缺点：这样成员变量的声明也要随着初始化列表这样的顺序进行
			//修改3
		{
			_capacity = strlen(str);
			_size = _capacity;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		//2.无参数的构造函数
		//string()
		//{
		//	//_capacity = 0;
		//	//_size = 0;
		//	//_str = nullptr;
		//	//如果str的值设为空，那么低下会造成空指针的解引用
		//	//所以：
		//	_capacity = 0;
		//	_size = 0;
		//	_str = new char[1];
		//	_str = '\0';
		//}
		//这样还是麻烦，所以我们一般在构造函数中，做全缺省
		//3.析构函数
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_capacity = 0;
			_size = 0;
		}

		const char* c_str()const
		{
			return _str;
		}
		size_t size()const
		{
			return _size;
		}
		char& operator[](size_t pos)
		{
			return _str[pos];
		}
		//const
		const char& operator[](size_t pos)const
		{
			return _str[pos];
		}

		//reserve开空间
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n];
				strcpy(tmp, _str);
				delete[]_str;
				_str = tmp;
				_capacity = n;
			}
		}
		//尾插字符
		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}
		string& operator+=(char ch)
		{
			push_back(ch);
		}
		//尾插字符串
		void append(const char* str)
		{
			int len = strlen(str);
			if (len + _size > _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str + _size, str);
			_size += len;
		}
		string& operator+=(const char* str)
		{
			append(str);
		}
		//在pos位置插入insert
		////插入字符
		void insert(size_t pos, char ch)
		{
			if (_size == _capacity)
			{
				int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + 1] = _str[end];
				end--;
			}
			_str[pos] = ch;
			_size++;
		}
		////插入字符串
		void insert(size_t pos, char* str)
		{
			size_t len = strlen(str);
			if (len + _size > _capacity)
			{
				reserve(_size + len);
			}
			int end = _size;
			while (end >= (int)(pos + len))
			{
				_str[end + 1] = _str[end];
				end--;
			}
			strncpy(_str + pos, str, len);
			_size += len;
		}
		//删除pos到len位置的字符
		void erase(size_t pos, size_t len=npos)
		{
			if (len == npos || len + pos > _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
		}
		//交换swap
		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}
		//查找字符find
		int find(char ch, size_t pos = 0)
		{
			for (int i = 0; i < _size; i++)
			{
				if (ch == _str[i])
				{
					return i;
				}
			}
			return npos;
		}
		//查找字符串find
		int find(char* str, size_t pos = 0)
		{
			const char* ptr = strstr(_str + pos, str);
			if (ptr == nullptr)
			{
				return npos;
			}
			else
			{
				return ptr - _str;
			}
		}
		//提取pos到len位置的字符substr
		string substr(size_t pos = 0, size_t len = npos)
		{
			size_t end = pos + len;
			if (len == npos || end > _size)
			{
				end = _size;
			}
			string str;
			str.reserve(end - pos);
			for (int i = 0; i < end; i++)
			{
				str += _str[i];
			}
			return str;
		}
	private:
		size_t _capacity;
		size_t _size;
		char* _str;
		const static size_t npos=-1;
	};
}

void test_string1()
{
	string s1("hello world");
	cout << s1.c_str() << endl;

	string s2;
	cout << s2.c_str() << endl;

	for (size_t i = 0; i < s1.size(); i++)
	{
		s1[i]++;
	}
	cout << s1.c_str() << endl;

	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	// ɵʽ滻ɵ
	for (auto ch : s1)
	{
		cout << ch << " ";
	}
	cout << endl;
	s1.insert(5, "xxxxxxxx");
	cout << s1 << endl;

	string s5("wangyehui");
	cout << s5 << endl;
	s5.erase(4, 2);
	cout << s5 << endl;
	string s6("wangyehui");
	string s7("yangjuntao");
	s6.swap(s7);
	cout << s6 << endl;
}